package com.ruoyi.management.mock.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.management.basics.controller.BaseExtController;
import com.ruoyi.management.mock.ro.*;
import com.ruoyi.management.mock.service.ITNoticeService;
import com.ruoyi.management.mock.vo.GetNoticeListVo;
import com.ruoyi.system.service.ISysDeptService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 公告Controller
 * 
 * @author lihy
 * @date 2021-11-29
 */
@RestController
@RequestMapping("/mock")
public class TNoticeController extends BaseExtController
{
    @Resource
    private ITNoticeService tNoticeService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询公告列表 带分页
     */
    @ApiOperation(value = " 查询公告列表 带分页", notes = "查询公告列表 带分页")
    @GetMapping("/get_notice_list")
    public TableDataInfo list(@Valid GetNoticeListParam param) {
        Long companyId = getCompanyId();
        if (null != companyId)
        {
            param.setCompanyId(companyId);
        }
        startPage();
        List<GetNoticeListVo> list = tNoticeService.selectTNoticeList(param);
        return getDataTable(list);
    }


    /**
     * 获取公告详细信息
     */
    @ApiOperation(value = " 获取公告详细信息", notes = "获取公告详细信息")
    @GetMapping(value = "/get_notice_byId")
    public AjaxResult getInfo(@Valid GetNoticeByIdParam param) {
        return AjaxResult.success(tNoticeService.selectTNoticeById(param));
    }

    /**
     * 新增公告
     */
    @ApiOperation(value = " 新增公告", notes = "新增公告")
    @PostMapping("/add_notice")
    public AjaxResult addTNotice(@RequestBody AddNoticeParam param) {
        LoginUser user = getLoginUser();
        param.setCreateUser(user.getUserId());
        param.setDeptId(user.getDeptId());
        return toAjax(tNoticeService.addTNotice(param));
    }

    /**
     * 新增公告(企业)
     */
    @ApiOperation(value = " 新增公告", notes = "新增公告")
    @PostMapping("/add_notice_company")
    public AjaxResult companyAddTNotice(@RequestBody AddNoticeParam param) {
        LoginUser user = getLoginUser();
        param.setCreateUser(user.getUserId());
        param.setDeptId(user.getDeptId());
        SysDept dept = deptService.selectDeptById(user.getDeptId());
        if (null != dept && null != dept.getCompanyId())
        {
            param.setCompanyId(dept.getCompanyId());
        }
        param.setIsTop("N");
        param.setIsHot("N");
        param.setIsBanner("N");
        return toAjax(tNoticeService.addTNotice(param));
    }

    /**
     * 修改公告
     */
    @ApiOperation(value = "修改公告", notes = "修改公告")
    @PutMapping("/update_notice")
    public AjaxResult updateNotice(@RequestBody UpdateNoticeParam param) {
        return toAjax(tNoticeService.updateTNotice(param));
    }

    /**
     * 修改公告
     */
    @ApiOperation(value = "修改公告", notes = "修改公告")
    @PutMapping("/update_notice_company")
    public AjaxResult updateNoticeCompany(@RequestBody UpdateNoticeParam param) {
        LoginUser user = getLoginUser();
        param.setCreateUser(user.getUserId());
        param.setDeptId(user.getDeptId());
        SysDept dept = deptService.selectDeptById(user.getDeptId());
        if (null != dept && null != dept.getCompanyId())
        {
            param.setCompanyId(dept.getCompanyId());
        }
        param.setIsTop("N");
        param.setIsHot("N");
        param.setIsBanner("N");
        return toAjax(tNoticeService.updateTNotice(param));
    }

    /**
     * 公告发布/下架
     */
    @ApiOperation(value = "公告发布/下架", notes = "公告发布/下架")
    @PostMapping("/notice_shelves")
    public AjaxResult noticeShelves(@Valid @RequestBody NoticeShelvesParam param) {
        return toAjax(tNoticeService.noticeShelves(param));
    }
}
